import boss from '../canvas/boss'
import bullet from '../canvas/bullet'
import player from '../canvas/player'
import steel from '../canvas/steel'
import tank from '../canvas/tank'
import wall from '../canvas/wall'

import config from '../config'
import { DirectionEnum } from '../enum/directionEnum'
import { image } from '../service/image'
import util from '../util'
import ModelAbstract from './modelAbstract'

export default class BulletModel extends ModelAbstract implements IModel {
  canvas: ICanvas = bullet
  name: string = 'bullet'

  constructor(public tank: IModel) {
    super(tank.x + config.model.width / 2, tank.y + config.model.height / 2)
    this.direction = tank.direction as unknown as DirectionEnum
  }

  image(): HTMLImageElement {
    return image.get('bullet')!
  }

  render(): void {
    let x = this.x
    let y = this.y
    // 子弹速度
    const step: number = this.tank.name === 'player' ? 10 : 5

    switch (this.direction) {
      case DirectionEnum.Top:
        y -= step
        break

      case DirectionEnum.Right:
        x += step
        break

      case DirectionEnum.Bottom:
        y += step
        break

      case DirectionEnum.Left:
        x -= step
        break
    }

    // 模型碰撞检测
    const touchModel = util.isModelTouch(x, y, 2, 2, [
      ...wall.models,
      ...steel.models,
      ...boss.models,
      ...tank.models,
      ...player.models,
    ])

    // 触碰画布边缘检测
    if (util.isCanvasTouch(x, y, 2, 2)) {
      this.destory()
    } else if (touchModel && touchModel.name !== this.tank.name) {
      this.destory()
      if (touchModel.name !== 'steel') touchModel.destory()
      this.blast(touchModel)
    } else {
      this.x = x
      this.y = y
      this.draw()
    }
  }

  protected draw() {
    this.canvas.ctx.drawImage(this.image(), this.x, this.y, 2, 2)
  }
}
